package cn.stylefeng.guns.modular.transport.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.core.util.QRCodeUtil;
import cn.stylefeng.guns.modular.basic.model.params.BasicDriverParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicOperateParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicOperateResult;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.transport.entity.TransportStevedoreSignin;
import cn.stylefeng.guns.modular.transport.model.params.TransportStevedoreSigninParam;
import cn.stylefeng.guns.modular.transport.service.TransportStevedoreSigninService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 装卸签到表控制器
 *
 * @author zhucx
 * @Date 2023-11-04 15:48:31
 */
@Controller
@RequestMapping("/transportStevedoreSignin")
public class TransportStevedoreSigninController extends BaseController {

    private String PREFIX = "/transport/transportStevedoreSignin";

    @Autowired
    private TransportStevedoreSigninService transportStevedoreSigninService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicOperateService basicOperateService;

    @RequestMapping("/toqrCode")
    public String toqrCode() {
        return PREFIX + "/qrCode.html";
    }

    /**
     * 创建二维码
     */	
    @RequestMapping(value = "/getqrcode", method = RequestMethod.GET)
    @ResponseBody
    public void generateQRCode4Product(Integer id,String name, HttpServletRequest request, HttpServletResponse response) {
    	User user = userService.getcurrentUser();
    	BasicOperateParam param = new BasicOperateParam();
    	param.setDeptId(user.getDeptId());
    	BasicOperateResult basicOperateResult = basicOperateService.findBySpec(param,user.getOperateId());
    	if(basicOperateResult != null) {
    		id = basicOperateResult.getId();
    		try {
    			//logo
    			File logoFile = new File("");
    			
    			String url = "https://jilu56it.top//stevedore_signin?operateId="+id;
    			
    			QRCodeUtil.drawLogoQRCode(logoFile, url, name,350,350,request,response);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }

    /**
     * 跳转到主页面
     *
     * @author zhucx
     * @Date 2023-11-04
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/transportStevedoreSignin.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2023-11-04
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/transportStevedoreSignin_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2023-11-04
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/transportStevedoreSignin_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2023-11-04
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(TransportStevedoreSigninParam transportStevedoreSigninParam) {
        this.transportStevedoreSigninService.add(transportStevedoreSigninParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2023-11-04
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(TransportStevedoreSigninParam transportStevedoreSigninParam) {
        this.transportStevedoreSigninService.update(transportStevedoreSigninParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhucx
     * @Date 2023-11-04
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(TransportStevedoreSigninParam transportStevedoreSigninParam) {
        this.transportStevedoreSigninService.delete(transportStevedoreSigninParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2023-11-04
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(TransportStevedoreSigninParam transportStevedoreSigninParam) {
        TransportStevedoreSignin detail = this.transportStevedoreSigninService.getById(transportStevedoreSigninParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2023-11-04
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(TransportStevedoreSigninParam transportStevedoreSigninParam,String condition ,String startTime,String endTime) {
    	User user = userService.getcurrentUser();
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		transportStevedoreSigninParam.setSigninTimes("'"+startTime+"' and '"+endTime+"'");
		} else if (startTime == null && endTime == null){
			startTime = DateUtil.getDate();
			endTime = DateUtil.getDate();
			transportStevedoreSigninParam.setSigninTimes("'"+startTime+"' and '"+endTime+"'");
		}
    	transportStevedoreSigninParam.setDeptId(user.getDeptId());
        return this.transportStevedoreSigninService.findPageBySpec(transportStevedoreSigninParam,condition,user.getOperateId());
    }

}


